//
//  XGL_PersonalPublishLabelView.swift
//  XGamesLive
//
//  Created by ios on 2024/5/21.
//

import UIKit
import CollectionKit

struct PublishLabelData {
    var title: String = ""
    var isSelected: Bool = false
}

class XGL_PersonalPublishLabelView: UIView {
    private var collectionView: CollectionView!
    private var markDatas:  ArrayDataSource<PublishLabelData> = ArrayDataSource(data: [])
    
    var labels: [PublishLabelData] = []{
        didSet{
            self.reloadDatas()
        }
    }
    var selectItems: [String]{
        return self.labels.filter({$0.isSelected}).map({$0.title})
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.collectionView = CollectionView()
        self.addSubview(self.collectionView)
        self.collectionView.snp.makeConstraints { make in
            make.top.centerX.bottom.equalToSuperview()
            make.left.equalTo(8.0)
        }
        self.snp.makeConstraints { make in
            make.height.equalTo(0)
        }
        self.setCollectionData()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.reloadDatas()
    }
}

extension XGL_PersonalPublishLabelView{
    @objc private func selectItemAction(sender: UIButton){
        let index = sender.tag - 1500
        self.labels[index].isSelected = !self.labels[index].isSelected
        self.reloadDatas()
    }
}

extension XGL_PersonalPublishLabelView{
    private func reloadDatas(){
        self.markDatas.data = self.labels
        self.collectionView.layoutIfNeeded()
        self.layoutIfNeeded()
        var width: CGFloat = 0.0
        var offsetY: CGFloat = self.labels.count > 0 ? 1 : 0
        for i in 0 ..< self.labels.count{
            width += self.collectionView.provider!.frame(at: i).width
            if width > self.collectionView.width{
                width = self.collectionView.provider!.frame(at: i).width
                offsetY += 1
            }
        }
        self.snp.updateConstraints({ (make) in
            make.height.equalTo(44.0 * offsetY)
        })
    }
    

    private func setCollectionData(){
        let viewSource = ClosureViewSource(viewUpdater: { [weak self] (view: UIView, data: PublishLabelData, index: Int) in
            guard let `self` = self else { return }
            view.backgroundColor = .clear
            view.removeSubviews()
            let titleText = PanPaddingLabel().then { make in
                make.text = data.title
                make.textColor = data.isSelected ? UIColor.hexColor("#191A1D") : UIColor.hexColor("#80FFE6")
                make.font = UIFont.pingfangSC_Regular(fontSize: 16.0)
                make.backgroundColor = data.isSelected ? UIColor.hexColor("#80FFE6") : UIColor.hexColor("#80FFE6").withAlphaComponent(0.1)
                make.textInsets = UIEdgeInsets(top: 9, left: 20, bottom: 9, right: 20)
                make.layerCornerRadius = 4.0
            }
            let button = UIButton().then { make in
                make.tag = index + 1500
                make.addTarget(self, action: #selector(self.selectItemAction(sender:)), for: .touchUpInside)
            }
            view.addSubview(titleText)
            view.addSubview(button)
            titleText.snp.makeConstraints { make in
                make.center.equalToSuperview()
            }
            button.snp.makeConstraints { make in
                make.edges.equalTo(titleText)
            }
        })
        let sizeSource = { (index: Int, data: PublishLabelData, collectionSize: CGSize) -> CGSize in
            let width = data.title.width(font: UIFont.pingfangSC_Regular(fontSize: 16.0)) + 54.0
            return CGSize(width: width, height: 44.0)
        }
        let provider = BasicProvider(
            dataSource: self.markDatas,
            viewSource: viewSource,
            sizeSource: sizeSource
        )
        self.collectionView.provider = provider
    }

}
